##Replication code for "drivers of resemblance: explaining the conversion of pre-electoral coalitions
##into coalition cabinets"
library(QCA)
library(tidyverse)
library(ragg)
library(SetMethods)
library(showtext)

##Cluster Analysis on Electoral Rules----
sec_ec <- read.csv("dpr.csv", sep=";")  %>%
          filter(type == "pre") %>%
          select(-type, -inter_party_primaries,
                 -low_chamber, -upp_chamber, -status, -ce, -country)

sec_ec1 <- sec_ec %>%
           select(-legislative_type)  %>%
           column_to_rownames(var = "cases") 

sec_ec2 <- sec_ec1 %>%
           mutate(lwpol = calibrate(lwpol, type = "fuzzy", method = "direct", threshold = "e=3, c=2, i=1")) %>%
           mutate(hlpol = calibrate(hlpol, type = "fuzzy", method = "direct", threshold = "e=1.5, c=2.7, i=4")) %>%
           mutate(htemp = calibrate(htemp, type = "fuzzy", method = "direct", threshold = "e=85, c=70, i=55")) %>%
           mutate(lplp = calibrate(lplp, type = "fuzzy", method = "direct", threshold = "e=0.5, c=0.405, i=0.3")) %>%
           rename(LWPOL = lwpol,
                  MAJ = maj, 
                  HLPOL = hlpol,
                  HTEMP = htemp,
                  LPP = lplp,
                  CR = cr)

##Analysis of Sufficiency
TTsec1 <- truthTable(sec_ec2, outcome = "CR", conditions = c("MAJ", "LWPOL", "HLPOL", "HTEMP", "LPP"),
                     complete = TRUE, incl.cut = 0.8,
                     sort.by = "incl, n+",
                     show.cases = TRUE)

ESA <- findRows("~MAJ*~HTEMP", obj = TTsec1)

intermediate_result <- minimize(TTsec1, include = "?", details = TRUE, dir.exp = "1,1,1,1,1", exclude = ESA)

sec_cc <- sec_ec %>% 
          mutate(lwpol = calibrate(lwpol, type = "fuzzy", method = "direct", threshold = "e=3, c=2, i=1")) %>%
          mutate(hlpol = calibrate(hlpol, type = "fuzzy", method = "direct", threshold = "e=1.5, c=2.7, i=4")) %>%
          mutate(htemp = calibrate(htemp, type = "fuzzy", method = "direct", threshold = "e=85, c=70, i=55")) %>%
          mutate(lplp = calibrate(lplp, type = "fuzzy", method = "direct", threshold = "e=0.5, c=0.405, i=0.3")) %>%
          rename(LWPOL = lwpol,
                 MAJ = maj, 
                 HLPOL = hlpol,
                 HTEMP = htemp,
                 LPP = lplp,
                 CR = cr)

##The code below produces Table A.10 in the Supplementary Material.
sec_cc %>% cluster(results = intermediate_result,
                   outcome = "CR",
                   unit_id = "cases",
                   cluster_id = "legislative_type")

##Cluster Analysis on Coalition Primaries----
sec_mp <- read.csv("dpr.csv", sep=";")  %>%
          filter(type == "pre") %>%
          select(-type, -legislative_type,
                 -low_chamber, -upp_chamber, -status, -ce, -country)

sec_mp1 <- sec_mp %>%
           column_to_rownames(var = "cases")

sec_mp2 <- sec_mp1 %>%
           mutate(lwpol = calibrate(lwpol, type = "fuzzy", method = "direct", threshold = "e=3, c=2, i=1")) %>%
           mutate(hlpol = calibrate(hlpol, type = "fuzzy", method = "direct", threshold = "e=1.5, c=2.7, i=4")) %>%
           mutate(htemp = calibrate(htemp, type = "fuzzy", method = "direct", threshold = "e=85, c=70, i=55")) %>%
           mutate(lplp = calibrate(lplp, type = "fuzzy", method = "direct", threshold = "e=0.5, c=0.405, i=0.3")) %>%
           rename(LWPOL = lwpol,
                  MAJ = maj, 
                  HLPOL = hlpol,
                  HTEMP = htemp,
                  LPP = lplp,
                  CR = cr)

##Analysis of Sufficiency
TTsec1 <- truthTable(sec_mp2, outcome = "CR", conditions = c("MAJ", "LWPOL", "HLPOL", "HTEMP", "LPP"),
                     complete = TRUE, incl.cut = 0.8,
                     sort.by = "incl, n+",
                     show.cases = TRUE)

ESA <- findRows("~MAJ*~HTEMP", obj = TTsec1)

intermediate_result <- minimize(TTsec1, include = "?", details = TRUE, dir.exp = "1,1,1,1,1", exclude = ESA)


sec_mpc <- sec_mp %>% 
           mutate(lwpol = calibrate(lwpol, type = "fuzzy", method = "direct", threshold = "e=3, c=2, i=1")) %>%
           mutate(hlpol = calibrate(hlpol, type = "fuzzy", method = "direct", threshold = "e=1.5, c=2.7, i=4")) %>%
           mutate(htemp = calibrate(htemp, type = "fuzzy", method = "direct", threshold = "e=85, c=70, i=55")) %>%
           mutate(lplp = calibrate(lplp, type = "fuzzy", method = "direct", threshold = "e=0.5, c=0.405, i=0.3")) %>%
           rename(LWPOL = lwpol,
                  MAJ = maj, 
                  HLPOL = hlpol,
                  HTEMP = htemp,
                  LPP = lplp,
                  CR = cr)

##The code below produces Table A.11 in the Supplementary Material. 
sec_mpc %>% cluster(results = intermediate_result,
                    outcome = "CR",
                    unit_id = "cases",
                    cluster_id = "inter_party_primaries")


##Cluster Analysis on Concurrent Elections----
rob_2_sec <- read.csv("dpr.csv", sep=";") %>%
             filter(type != "electoral") %>%
             select(-type, -legislative_type, -inter_party_primaries,
                    -low_chamber, -upp_chamber, -status, -country)
 
rob_2_sec1 <- rob_2_sec %>%
              select(-ce) %>%
              column_to_rownames(var = "cases")

rob_2_sec2 <- rob_2_sec1 %>%
              mutate(lwpol = calibrate(lwpol, type = "fuzzy", method = "direct", threshold = "e=3, c=2, i=1")) %>%
              mutate(hlpol = calibrate(hlpol, type = "fuzzy", method = "direct", threshold = "e=1.5, c=2.7, i=4")) %>%
              mutate(htemp = calibrate(htemp, type = "fuzzy", method = "direct", threshold = "e=85, c=70, i=55")) %>%
              mutate(lplp = calibrate(lplp, type = "fuzzy", method = "direct", threshold = "e=0.5, c=0.405, i=0.3")) %>%
              rename(LWPOL = lwpol,
                     MAJ = maj, 
                     HLPOL = hlpol,
                     HTEMP = htemp,
                     LPP = lplp,
                     CR = cr)

##Analysis of Sufficiency
TTsec1 <- truthTable(rob_2_sec2, outcome = "CR", conditions = c("MAJ", "LWPOL", "HLPOL", "HTEMP", "LPP"),
                     complete = TRUE, incl.cut = 0.8,
                     sort.by = "incl, n+",
                     show.cases = TRUE)

ESA <- findRows("~MAJ*~HTEMP", obj = TTsec1)

intermediate_result <- minimize(TTsec1, include = "?", details = TRUE, dir.exp = "1,1,1,1,1", exclude = ESA)

rob_2_sec_3 <- rob_2_sec %>% 
               mutate(lwpol = calibrate(lwpol, type = "fuzzy", method = "direct", threshold = "e=3, c=2, i=1")) %>%
               mutate(hlpol = calibrate(hlpol, type = "fuzzy", method = "direct", threshold = "e=1.5, c=2.7, i=4")) %>%
               mutate(htemp = calibrate(htemp, type = "fuzzy", method = "direct", threshold = "e=85, c=70, i=55")) %>%
               mutate(lplp = calibrate(lplp, type = "fuzzy", method = "direct", threshold = "e=0.5, c=0.405, i=0.3")) %>%
               rename(LWPOL = lwpol,
                      MAJ = maj, 
                      HLPOL = hlpol,
                      HTEMP = htemp,
                      LPP = lplp,
                      CR = cr,
                      CE = ce)

##The code below produces Table A.12 in the Supplementary Material
rob_2_sec_3 %>% cluster(results = intermediate_result,
                        outcome = "CR",
                        unit_id = "cases",
                        cluster_id = "CE")

##Cluster Analysis on Country----
rob_3_sec <- read.csv("dpr.csv", sep=";") %>%
             filter(type != "electoral") %>%
             select(-type, -legislative_type, -inter_party_primaries,
                    -low_chamber, -upp_chamber, -status)
 
rob_3_sec1 <- rob_3_sec %>%
              select(-country) %>%
              column_to_rownames(var = "cases")

rob_3_sec2 <- rob_3_sec1 %>%
              mutate(lwpol = calibrate(lwpol, type = "fuzzy", method = "direct", threshold = "e=3, c=2, i=1")) %>%
              mutate(hlpol = calibrate(hlpol, type = "fuzzy", method = "direct", threshold = "e=1.5, c=2.7, i=4")) %>%
              mutate(htemp = calibrate(htemp, type = "fuzzy", method = "direct", threshold = "e=85, c=70, i=55")) %>%
              mutate(lplp = calibrate(lplp, type = "fuzzy", method = "direct", threshold = "e=0.5, c=0.405, i=0.3")) %>%
              rename(LWPOL = lwpol,
                     MAJ = maj, 
                     HLPOL = hlpol,
                     HTEMP = htemp,
                     LPP = lplp,
                     CR = cr)

##Analysis of Sufficiency
TTsec1 <- truthTable(rob_3_sec2, outcome = "CR", conditions = c("MAJ", "LWPOL", "HLPOL", "HTEMP", "LPP"),
                     complete = TRUE, incl.cut = 0.8,
                     sort.by = "incl, n+",
                     show.cases = TRUE)

ESA <- findRows("~MAJ*~HTEMP", obj = TTsec1)

intermediate_result <- minimize(TTsec1, include = "?", details = TRUE, dir.exp = "1,1,1,1,1", exclude = ESA)

rob_3_sec_3 <- rob_3_sec %>% 
               mutate(lwpol = calibrate(lwpol, type = "fuzzy", method = "direct", threshold = "e=3, c=2, i=1")) %>%
               mutate(hlpol = calibrate(hlpol, type = "fuzzy", method = "direct", threshold = "e=1.5, c=2.7, i=4")) %>%
               mutate(htemp = calibrate(htemp, type = "fuzzy", method = "direct", threshold = "e=85, c=70, i=55")) %>%
               mutate(lplp = calibrate(lplp, type = "fuzzy", method = "direct", threshold = "e=0.5, c=0.405, i=0.3")) %>%
               rename(LWPOL = lwpol,
                      MAJ = maj, 
                      HLPOL = hlpol,
                      HTEMP = htemp,
                      LPP = lplp,
                      CR = cr)

##The code below produces Table A.13 in the Supplementary Material
rob_3_sec_3 %>% cluster(results = intermediate_result,
                        outcome = "CR",
                        unit_id = "cases",
                        cluster_id = "country")